import logging

import requests,json,pytest
def du():
    with open ('a.json','r',encoding='utf-8')as f:
        data=json.load(f)
        return data
class Test119():
    token = ""
    id = ""

    def setup_method(self):
        print("开始")

    def teardown(self):
        print("清理环境")

    # 登录
    @pytest.mark.parametrize('data',du())
    def test_login(self, data):
        # import pymysql
        # conn=pymysql.connect(user="root",password="ceshi666666",host="10.59.9.21",database='a3mall')
        # cur=conn.cursor()
        # cur.execute("select username from mall_users")
        # res=cur.fetchall()
        # print(res)
        try:

            url = "http://10.59.9.12:7001/api/public/login"
            headers = {"content-type": "application/json"}
            body = {"username":data.get('username'), "password": "123456"}
            r = requests.post(url=url, headers=headers, json=body)
            Test119.token = r.json()['data']['token']
            assert r.status_code == 200
            print(r.json())
        except Exception as E:
            logging.info(E)
        assert r.status_code == 200

    # 查看商品详情
    def test_order(self):
        try:
            url = "http://10.59.9.12:7001/api/goods/view?id=123"
            headers = {"auth-token": "Bearer " + Test119.token}
            r = requests.get(url=url, headers=headers)
            print(r.json())
        except Exception as E:
            logging.info(E)
        assert '好吃' in r.text

    # 加入购物车
    def test_order_gwc(self):
        try:

            url = "http://10.59.9.12:7001/api/cart/add"
            headers = {"auth-token": "Bearer " + Test119.token, "content-type": "application/json"}
            body = {"id": 123, "sku_id": "", "num": 1}
            r = requests.post(url=url, headers=headers, json=body)
            print(r.json())
        except Exception as E:
            logging.info(E)
        assert r.json()['info'] == '商品添加至购物车成功'

    # # 确定订单
    def test_order_sure(self):
        try:

            url = "http://10.59.9.12:7001/api/cart?page=1"
            headers = {"auth-token": "Bearer " + Test119.token}
            r = requests.get(url=url, headers=headers)
            Test119.id = r.json()['data']['list'][0]['id']
            print(r.json())
        except Exception as E:
            logging.info(E)
        assert r.status_code == 200

    # # 提交订单
    def test_order_tijiao(self):
        try:
            url = "http://10.59.9.12:7001/api/order/create"
            body = {"id":Test119.id,"type":"cart","address_id":343,"store_id":2,"shipping_type":1,"bonus_id":"0","payment":"balance","remarks":"","source":1,"url":"http://10.59.9.12:7001/wap/pages/cart/confirm?id=2192&type=cart"}
            headers = {"auth-token": "Bearer " + Test119.token, "content-type": "application/json"}
            r = requests.post(url=url, headers=headers, json=body)
            print(r.json())
            assert r.status_code == 200
        except Exception as E:
            logging.info(E)

#商品id不存在，提交订单失败，提示“请选择需要购买的商品”
    def test_order_tijiao1(self):
        try:

            url = "http://10.59.9.12:7001/api/order/create"
            body = {"id":"", "type": "cart", "address_id": 343, "store_id": 2, "shipping_type": 1, "bonus_id": "0",
                    "payment": "balance", "remarks": "", "source": 1,
                    "url": "http://10.59.9.12:7001/wap/pages/cart/confirm?id=2192&type=cart"}
            headers = {"auth-token": "Bearer " + Test119.token, "content-type": "application/json"}
            r = requests.post(url=url, headers=headers, json=body)
            print(r.json())
            assert r.json()['info']=='请选择需要购买的商品'
        except Exception as E:
            logging.info(E)




